package nina.yiyou.attendance.s10merge.compute.offgain

import nina.yiyou.attendance.s10merge.compute.AttendDetailMerger
import nina.yiyou.attendance.s10merge.compute.latepoint.LatePointCalculatorFactory
import nina.yiyou.salary.input.config.WorkTimeSheetReader

import java.time.LocalDate
import java.time.LocalDateTime

class OffGainCalculatorFactory {

  OffGainCalculatorFactory(AttendDetailMerger.PunchDay day, List<AttendDetailMerger.PunchDay> dayList, List<WorkTimeSheetReader.Line> workTimeCfg) {
    _day = day
    _dayList = dayList
    _workTimeCfg = workTimeCfg
  }

  OffGainCalculator create() {
    return new OffGainCalculator(_day, makeNext())
  }

  private OffGainCalculator.NextDay makeNext() {
    LocalDateTime shouldIn = new DeltaDayFinder(_day, { LocalDate d -> d.plusDays(1) }, _dayList).find()
        .map { new LatePointCalculatorFactory(it, _dayList, _workTimeCfg).create() }
        .map { it.calc() }
        .orElse(null)

    return new OffGainCalculator.NextDay() {
      @Override
      LocalDateTime latePoint() {
        return shouldIn
      }
    }
  }

  private final AttendDetailMerger.PunchDay _day

  private final List<AttendDetailMerger.PunchDay> _dayList
  private final List<WorkTimeSheetReader.Line> _workTimeCfg
}
